package com.jmpt.fangbohui.ui.base;

import android.support.v4.app.Fragment;

import com.loopj.android.http.RequestHandle;

import java.util.ArrayList;
import java.util.List;

public class BaseFragment extends Fragment {
	/**
     * 收集界面中产生的<b>http请求对象</b><br/>
     * 通过<b>addRequestHandle(RequestHandle requestHandle)</b>方法添加<br/>
     * 当activity被<b>onDestroyView()</b>时，统一cancel这些请求，无论是请求完毕还是正在请求中的。
     */
    public List<RequestHandle> mRequestHandles;

    /**
     * 把页面中所有产生的http请求对象RequestHandle收集起来,待activity被onDestroyView()时，统一cancel释放
     * <pre>
	    protected void onDestroy() {
	        super.onDestroy();
	        if (mRequestHandles != null && mRequestHandles.size() > 0) {
	            for (int i = 0; i < mRequestHandles.size(); i++) {
	                RequestHandle requestHandle = mRequestHandles.get(i);
	                if ((requestHandle.isFinished() || requestHandle.isCancelled()) == false) {
	                    requestHandle.cancel(true);
	                }
	            }
	        }
	    }
     * </pre>
     * @param requestHandle
     */
    public void addRequestHandle(RequestHandle requestHandle) {
        if (mRequestHandles == null) {
            mRequestHandles = new ArrayList<RequestHandle>();
        }
        mRequestHandles.add(requestHandle);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mRequestHandles != null && mRequestHandles.size() > 0) {
            for (int i = 0; i < mRequestHandles.size(); i++) {
                RequestHandle requestHandle = mRequestHandles.get(i);
                if ((requestHandle.isFinished() || requestHandle.isCancelled()) == false) {
                    requestHandle.cancel(true);
                }
            }
        }
    }


}
